/**
 * 
 */
package com.socialnetwork.security.pwd;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author asoundar
 * 
 */
public class PwdEncryptor {

	public static final String PASSWORDS_ENCRYPTION_ALGORITHM = "SHA";

	public static final String UTF8 = "UTF-8";

	public static String encrypt(String clearTextPassword) {
		byte[] bytes = digestRaw(PASSWORDS_ENCRYPTION_ALGORITHM,
				clearTextPassword);

		return Base64.encode(bytes);

	}

	private static byte[] digestRaw(String algorithm, String... text) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance(algorithm);

			StringBuilder sb = new StringBuilder(text.length * 2 - 1);

			for (String t : text) {
				if (sb.length() > 0) {
					sb.append(":");
				}

				sb.append(t);
			}

			String s = sb.toString();

			messageDigest.update(s.getBytes(UTF8));
		} catch (NoSuchAlgorithmException nsae) {
		} catch (UnsupportedEncodingException uee) {
		}

		return messageDigest.digest();
	}
}
